package com.ruoyi.household.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.household.domain.Household;
import com.ruoyi.household.dto.HouseholdDto;
import com.ruoyi.household.mapper.HouseholdMapper;
import com.ruoyi.household.service.IBuildingSystemService;
import com.ruoyi.household.service.IHouseholdService;
import com.ruoyi.household.service.IResidentialQuartersService;
import com.ruoyi.system.api.RemoteNucleicAcidUserService;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.BuildingSystem;
import com.ruoyi.system.api.domain.ResidentialQuarters;
import com.ruoyi.system.api.domain.SysNucleicAcidUser;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 住户管理CRUDService业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-01
 */
@Service
public class HouseholdServiceImpl implements IHouseholdService
{
    @Autowired
    private HouseholdMapper householdMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteNucleicAcidUserService nucleicAcidUserService;

    @Autowired
    private IBuildingSystemService buildingSystemService;

    @Autowired
    private IResidentialQuartersService residentialQuartersService;

    /**
     * 查询住户管理CRUD
     * 
     * @param id 住户管理CRUD主键
     * @return 住户管理CRUD
     */
    @Override
    public Household selectHouseholdById(Long id)
    {
        return householdMapper.selectHouseholdById(id);
    }

    /**
     * 查询住户管理CRUD列表
     * 
     * @param household 住户管理CRUD
     * @return 住户管理CRUD
     */
    @Override
    public List<Household> selectHouseholdList(Household household)
    {

        return householdMapper.selectHouseholdList(household);
    }

    /**
     * 新增住户管理CRUD
     * 
     * @param dto 住户管理CRUD
     * @return 结果
     */
    @Override
    public int insertHousehold(HouseholdDto dto)
    {
        // 保存用户表
        SysUser sysUser = new SysUser();
        sysUser.setUserName(dto.getHouseholdName());
        sysUser.setNickName("若依");
        sysUser.setPhonenumber(dto.getHouseholdPhone());
        sysUser.setSex(dto.getSex());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setStatus("0");
        sysUser.setCreateTime(new Date());
        remoteUserService.add(sysUser, SecurityConstants.INNER);
        AjaxResult userId = remoteUserService.getUserId(dto.getHouseholdName());

        Object data = userId.get("data");

        Long aLong = Long.valueOf(String.valueOf(data));
        System.out.println(aLong);

        // 保存用户详情
        SysNucleicAcidUser nucleicAcidUser = new SysNucleicAcidUser();
        nucleicAcidUser.setuName(dto.getHouseholdName());
        nucleicAcidUser.setSex(dto.getSex());
        nucleicAcidUser.setPhonenumber(dto.getHouseholdPhone());
        nucleicAcidUser.setWechat(dto.getWechat());
        nucleicAcidUser.setRqName(dto.getRqName());
        nucleicAcidUser.setBuildingName(dto.getBuildingName());
        nucleicAcidUser.setHouseholdAddress(dto.getHouseholdAddress());
        nucleicAcidUserService.add(nucleicAcidUser, SecurityConstants.INNER);

        // 根据小区名字查询
        BuildingSystem buildingSystem = buildingSystemService.selectBuildingByName(dto.getRqName());
        ResidentialQuarters quarters = residentialQuartersService.selectResidentialQuartersByName(dto.getRqName());

        // 保存住户表
        Household household = new Household();
        household.setRqName(dto.getRqName());
        household.setBuildingName(dto.getBuildingName());
        household.setHouseholdName(dto.getHouseholdName());
        household.setHouseholdPhone(dto.getHouseholdPhone());
        household.setHouseholdAddress(dto.getHouseholdAddress());
        household.setHouseholdState("1");
        household.setHouseholdDescribe("无");
        household.setUserId(aLong);
        household.setBuildingId(buildingSystem.getId());
        household.setRqId(quarters.getId());

        return householdMapper.insertHousehold(household);
    }

    /**
     * 修改住户管理CRUD
     * 
     * @param household 住户管理CRUD
     * @return 结果
     */
    @Override
    public int updateHousehold(Household household)
    {
        return householdMapper.updateHousehold(household);
    }

    /**
     * 批量删除住户管理CRUD
     * 
     * @param ids 需要删除的住户管理CRUD主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdByIds(Long[] ids)
    {
        return householdMapper.deleteHouseholdByIds(ids);
    }

    /**
     * 删除住户管理CRUD信息
     * 
     * @param id 住户管理CRUD主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdById(Long id)
    {
        return householdMapper.deleteHouseholdById(id);
    }

    @Override
    public List<Household> getHousehold(String name) {
        return householdMapper.getHousehold(name);
    }

    @Override
    public Household getHouseholdName(String householdName) {
        return householdMapper.getHouseholdName(householdName);
    }

    @Override
    public Household getUser(Long id) {
        return householdMapper.getUser(id);
    }
}
